﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【消息】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto消息
    /// </summary>
    public abstract class AutoMessageMapping : BaseMapping<Message, MessageModel, MessageFilterModel>
    {
        public AutoMessageMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override MessageModel Entity2Model(Message entity)
        {
            MessageModel model = new MessageModel();

            model.MessageID = entity.MessageID;
            model.GroupInfoID = entity.GroupInfoID;
            model.Type = entity.Type;
            model.Title = entity.Title;
            model.ReceiveUserID = entity.ReceiveUserID;
            model.Data = entity.Data;
            model.Status = entity.Status;
            model.CreateDate = entity.CreateDate;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Message Model2Entity(MessageModel model)
        {
            Message entity = new Message();

            entity.MessageID = model.MessageID;
            entity.GroupInfoID = model.GroupInfoID;
            entity.Type = model.Type;
            entity.Title = model.Title;
            entity.ReceiveUserID = model.ReceiveUserID;
            entity.Data = model.Data;
            entity.Status = model.Status;
            entity.CreateDate = model.CreateDate;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Message> FilterModel2Where(MessageFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._MessageID.HasValue)
            {
                where = where.And(x => x.MessageID == filterModel._MessageID.Value);
            }
            if (filterModel._MessageIDs.IsNotEmpty() && filterModel._MessageIDs.Count > 0)
            {
                where = where.And(x => filterModel._MessageIDs.Contains(x.MessageID));
            }
            if (filterModel._NotMessageIDs.IsNotEmpty() && filterModel._NotMessageIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotMessageIDs.Contains(x.MessageID));
            }

            //集团
            if (filterModel._GroupInfoID.HasValue)
            {
                where = where.And(x => x.GroupInfoID == filterModel._GroupInfoID.Value);
            }
            if (filterModel._GroupInfoIDs.IsNotEmpty() && filterModel._GroupInfoIDs.Count > 0)
            {
                where = where.And(x => filterModel._GroupInfoIDs.Contains(x.GroupInfoID));
            }
            if (filterModel._NotGroupInfoIDs.IsNotEmpty() && filterModel._NotGroupInfoIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotGroupInfoIDs.Contains(x.GroupInfoID));
            }

            //类型
            if (filterModel._Type.HasValue)
            {
                where = where.And(x => x.Type == filterModel._Type.Value);
            }
            if (filterModel._Types.IsNotEmpty() && filterModel._Types.Count > 0)
            {
                where = where.And(x => filterModel._Types.Contains(x.Type));
            }
            if (filterModel._NotTypes.IsNotEmpty() && filterModel._NotTypes.Count > 0)
            {
                where = where.And(x => !filterModel._NotTypes.Contains(x.Type));
            }

            //状态
            if (filterModel._Status.HasValue)
            {
                where = where.And(x => x.Status == filterModel._Status.Value);
            }
            if (filterModel._Statuss.IsNotEmpty() && filterModel._Statuss.Count > 0)
            {
                where = where.And(x => filterModel._Statuss.Contains(x.Status));
            }
            if (filterModel._NotStatuss.IsNotEmpty() && filterModel._NotStatuss.Count > 0)
            {
                where = where.And(x => !filterModel._NotStatuss.Contains(x.Status));
            }

            //创建日期
            if (filterModel._CreateDate.HasValue)
            {
                where = where.And(x => x.CreateDate == filterModel._CreateDate.Value);
            }
            if (filterModel._CreateDate_Begin.HasValue)
            {
                where = where.And(x => x.CreateDate >= filterModel._CreateDate_Begin.Value);
            }
            if (filterModel._CreateDate_End.HasValue)
            {
                where = where.And(x => x.CreateDate < filterModel._CreateDate_End.Value.AddDays(1));
            }

            return where;
        }
        #endregion
    }
}

